import com.google.appengine.api.datastore.KeyFactory
import com.google.appengine.api.images.* 

if (params["id"]) {
	
 def id = Long.parseLong(params["id"])

 try {
   def key = KeyFactory.createKey("FotoPerfil", id)
   def foto = datastore.get(key)
   
   if (foto.imaxe) {
	   byte[] bytes = foto.imaxe.getBytes()
	   
	   def width = params['w']
       def height = params['h']
                           
       def ancho = 53
       def alto = 53
       
       if ( width && height ) {
		 ancho = Integer.parseInt(width) 
		 alto = Integer.parseInt(height) 
       }
	   
	   
       if ( width && height ){ 
    	     Image oldImage = ImagesServiceFactory.makeImage(bytes) 
    	   	 CompositeTransform cp = ImagesServiceFactory.makeCompositeTransform()
    	   	 Transform resize = ImagesServiceFactory.makeResize(ancho,alto)
    	   	 cp.concatenate( resize )
    	   	 Image pic = images.applyTransform( cp, oldImage, ImagesService.OutputEncoding.valueOf("JPEG"))
			 //response.setContentType("application/octet-stream")
    	   	 response.setContentType("image/jpeg")
			 sout << pic.imageData

       } 	   
	   
	   
	   
   } else {
	   println "no hay foto...."
   }

   } catch (Throwable t) {
	   println "error..........." + t
    //forward to some error page...
   }
} else {
	html.div {
		h1 ('no hay banda')
	}
}

	
